﻿using System; 
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Matrix.Common.DBUtility;
using Matrix.MaKaYunShang.Model;

namespace Matrix.MaKaYunShang.DAL
{
	public partial class McMessagesDAL : BaseDAL<McMessagesDAL>
	{
        #region 默认方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(McMessages model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into McMessages(");
            strSql.Append("MsgCate,MsgType,Title,Breif,Body,HerfUrl,Attachment,SendToAll,SendToGroup,SendToPrivate,Created,Updated,CreatedBy,UpdatedBy,Disabled,EmailFrom,SMSFrom,OrderId,IsDelete");
            strSql.Append(") values (");
            strSql.Append("@MsgCate,@MsgType,@Title,@Breif,@Body,@HerfUrl,@Attachment,@SendToAll,@SendToGroup,@SendToPrivate,@Created,@Updated,@CreatedBy,@UpdatedBy,@Disabled,@EmailFrom,@SMSFrom,@OrderId,@IsDelete");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
			            new SqlParameter("@MsgCate", SqlDbType.Int,4) ,            
                        new SqlParameter("@MsgType", SqlDbType.Int,4) ,            
                        new SqlParameter("@Title", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Breif", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Body", SqlDbType.NVarChar,-1) ,            
                        new SqlParameter("@HerfUrl", SqlDbType.NVarChar,150) ,            
                        new SqlParameter("@Attachment", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@SendToAll", SqlDbType.Bit,1) ,            
                        new SqlParameter("@SendToGroup", SqlDbType.Bit,1) ,            
                        new SqlParameter("@SendToPrivate", SqlDbType.Bit,1) ,            
                        new SqlParameter("@Created", SqlDbType.DateTime) ,            
                        new SqlParameter("@Updated", SqlDbType.DateTime) ,            
                        new SqlParameter("@CreatedBy", SqlDbType.Int,4) ,            
                        new SqlParameter("@UpdatedBy", SqlDbType.Int,4) ,            
                        new SqlParameter("@Disabled", SqlDbType.Bit,1) ,            
                        new SqlParameter("@EmailFrom", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@SMSFrom", SqlDbType.NVarChar,20) ,            
                        new SqlParameter("@OrderId", SqlDbType.Int,4) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };

            parameters[0].Value = model.MsgCate;
            parameters[1].Value = model.MsgType;
            parameters[2].Value = model.Title;
            parameters[3].Value = model.Breif;
            parameters[4].Value = model.Body;
            parameters[5].Value = model.HerfUrl;
            parameters[6].Value = model.Attachment;
            parameters[7].Value = model.SendToAll;
            parameters[8].Value = model.SendToGroup;
            parameters[9].Value = model.SendToPrivate;
            parameters[10].Value = model.Created;
            parameters[11].Value = model.Updated;
            parameters[12].Value = model.CreatedBy;
            parameters[13].Value = model.UpdatedBy;
            parameters[14].Value = model.Disabled;
            parameters[15].Value = model.EmailFrom;
            parameters[16].Value = model.SMSFrom;
            parameters[17].Value = model.OrderId;
            parameters[18].Value = model.IsDelete; var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            return obj == null ? 0 : Convert.ToInt32(obj);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(McMessages model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update McMessages set ");

            strSql.Append(" MsgCate = @MsgCate , ");
            strSql.Append(" MsgType = @MsgType , ");
            strSql.Append(" Title = @Title , ");
            strSql.Append(" Breif = @Breif , ");
            strSql.Append(" Body = @Body , ");
            strSql.Append(" HerfUrl = @HerfUrl , ");
            strSql.Append(" Attachment = @Attachment , ");
            strSql.Append(" SendToAll = @SendToAll , ");
            strSql.Append(" SendToGroup = @SendToGroup , ");
            strSql.Append(" SendToPrivate = @SendToPrivate , ");
            strSql.Append(" Created = @Created , ");
            strSql.Append(" Updated = @Updated , ");
            strSql.Append(" CreatedBy = @CreatedBy , ");
            strSql.Append(" UpdatedBy = @UpdatedBy , ");
            strSql.Append(" Disabled = @Disabled , ");
            strSql.Append(" EmailFrom = @EmailFrom , ");
            strSql.Append(" SMSFrom = @SMSFrom , ");
            strSql.Append(" OrderId = @OrderId , ");
            strSql.Append(" IsDelete = @IsDelete  ");
            strSql.Append(" where Id=@Id ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@MsgCate", SqlDbType.Int,4) ,            
                        new SqlParameter("@MsgType", SqlDbType.Int,4) ,            
                        new SqlParameter("@Title", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Breif", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Body", SqlDbType.NVarChar,-1) ,            
                        new SqlParameter("@HerfUrl", SqlDbType.NVarChar,150) ,            
                        new SqlParameter("@Attachment", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@SendToAll", SqlDbType.Bit,1) ,            
                        new SqlParameter("@SendToGroup", SqlDbType.Bit,1) ,            
                        new SqlParameter("@SendToPrivate", SqlDbType.Bit,1) ,            
                        new SqlParameter("@Created", SqlDbType.DateTime) ,            
                        new SqlParameter("@Updated", SqlDbType.DateTime) ,            
                        new SqlParameter("@CreatedBy", SqlDbType.Int,4) ,            
                        new SqlParameter("@UpdatedBy", SqlDbType.Int,4) ,            
                        new SqlParameter("@Disabled", SqlDbType.Bit,1) ,            
                        new SqlParameter("@EmailFrom", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@SMSFrom", SqlDbType.NVarChar,20) ,            
                        new SqlParameter("@OrderId", SqlDbType.Int,4) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.MsgCate;
            parameters[2].Value = model.MsgType;
            parameters[3].Value = model.Title;
            parameters[4].Value = model.Breif;
            parameters[5].Value = model.Body;
            parameters[6].Value = model.HerfUrl;
            parameters[7].Value = model.Attachment;
            parameters[8].Value = model.SendToAll;
            parameters[9].Value = model.SendToGroup;
            parameters[10].Value = model.SendToPrivate;
            parameters[11].Value = model.Created;
            parameters[12].Value = model.Updated;
            parameters[13].Value = model.CreatedBy;
            parameters[14].Value = model.UpdatedBy;
            parameters[15].Value = model.Disabled;
            parameters[16].Value = model.EmailFrom;
            parameters[17].Value = model.SMSFrom;
            parameters[18].Value = model.OrderId;
            parameters[19].Value = model.IsDelete; return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append("update McMessages set IsDelete=1 ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            var strSql = new StringBuilder();
            strSql.Append("update McMessages set IsDelete=1 ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            return DbHelperSQL.ExecuteSql(strSql.ToString()) > 0;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataSet GetModel(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append("select Id, MsgCate, MsgType, Title, Breif, Body, HerfUrl, Attachment, SendToAll, SendToGroup, SendToPrivate, Created, Updated, CreatedBy, UpdatedBy, Disabled, EmailFrom, SMSFrom, OrderId, IsDelete  ");
            strSql.Append("  from McMessages ");
            strSql.Append(" where Id=@Id  and IsDelete=0");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds.Tables[0].Rows.Count > 0 ? ds : null;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM McMessages where IsDelete=0 ");
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append(" and " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion
		
		#region 扩展方法
		#endregion
	}
}
